package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.yw.YwMoneyAffirmLog;
import com.example.ymm.modules1.entity.hl.yw.YwMoneyOrderNoStorage;
import com.example.ymm.modules1.mapper.hl.YwMoneyOrderNoStorageMapper;
import com.example.ymm.modules1.query.hl.YwMoneyAffirmLogQueryCriteria;
import com.example.ymm.modules1.query.hl.YwMoneyOrderNoStorageQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsMoneyAffirmDto;
import com.example.ymm.modules1.service.hl.YwMoneyAffirmLogService;
import com.example.ymm.modules1.service.hl.YwMoneyOrderNoStorageService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class YwMoneyOrderNoStorageServiceImpl extends BaseServiceImpl<YwMoneyOrderNoStorageMapper, YwMoneyOrderNoStorage> implements YwMoneyOrderNoStorageService {


    @Autowired
    YwMoneyAffirmLogService ywMoneyAffirmLogService;

    @Override
    public TableList<YwMoneyOrderNoStorage> list(YwMoneyOrderNoStorageQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<YwMoneyOrderNoStorage> arrs = queryAll(criteria);
        PageInfo<YwMoneyOrderNoStorage> pageInfo = new PageInfo<>(arrs);
        TableList<YwMoneyOrderNoStorage> tableList = new TableList(pageInfo);
        return tableList;
    }

    public List<YwMoneyOrderNoStorage> queryAll(YwMoneyOrderNoStorageQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(YwMoneyOrderNoStorage.class, criteria);
        predicate.orderByAsc("create_time");
        List<YwMoneyOrderNoStorage> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public List<MsMoneyAffirmDto> getByUserId(String userId, String type) {
        if(UtilValidate.isEmpty(userId)){
            throw new YmException("参数不能为空!");
        }

        YwMoneyOrderNoStorageQueryCriteria criteria=new YwMoneyOrderNoStorageQueryCriteria();
        criteria.setUserId(userId);
        criteria.setType(type);
        List<YwMoneyOrderNoStorage> arr = baseMapper.queryList(criteria);
        List<String> orderNoList=new ArrayList<>();
        for(YwMoneyOrderNoStorage arr1:arr){
            orderNoList.add(arr1.getOrderNo());
        }
        List<MsMoneyAffirmDto> msMoneyAffirmDtos =null;
        if(orderNoList.size()>0){
            YwMoneyAffirmLogQueryCriteria c2=new YwMoneyAffirmLogQueryCriteria();
            c2.setOrderNoIn(orderNoList);
            msMoneyAffirmDtos = ywMoneyAffirmLogService.msMoneyAffirmList(c2);
            for(MsMoneyAffirmDto dto: msMoneyAffirmDtos){
                dto.setStorageType(type);
            }
        }

        return msMoneyAffirmDtos;
    }
}
